package com.webproject.core;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.webproject.entity.Filters;
import com.webproject.entity.Rules;
import com.webproject.entity.sysManager.User;

public class BaseAction extends ActionSupport {
	protected Logger log=Logger.getLogger(getClass());
	/*
	 * 操作类型
	 * add,edit,del
	 */
	protected String oper; 
	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}
	protected int getCurrentPage(){
		String page=getRequest().getParameter("page");
		log.debug("page---------------:"+page);
		if(null!=page && !"".equals(page)){
			return Integer.parseInt(page);
		}
		return 0;
	}
	protected int getRows(){
		String rows=getRequest().getParameter("rows");
		log.debug("rows---------------:"+rows);
		if(null!=rows && !"".equals(rows)){
			return Integer.parseInt(rows);
		}
		return 0;
	}
	protected String getSidx(){
		String sidx=getRequest().getParameter("sidx");
		log.debug("sidx---------------:"+sidx);
		if(null!=sidx && !"".equals(sidx)){
			return sidx;
		}
		return "";
	}
	protected String getSord(){
		String sord=getRequest().getParameter("sord");
		log.debug("sord---------------:"+sord);
		if(null!=sord && !"".equals(sord)){
			return sord;
		}
		return "";
	}
	protected String getfilters(){
		return getRequest().getParameter("filters");
	}
	protected int startRecord(){
		return (getCurrentPage()-1)*getRows();
	}
	protected Page getPage(){
		Page page=new Page();
		page.setPage(String.valueOf(getCurrentPage()));
		page.setStartRecords(startRecord());
//		page.setRecords(String.valueOf(getRecords()));
		page.setRow(getRows());
		page.setSidx(getSidx());
		page.setSord(getSord());
		page.setFilters(getFilters());
		return page;
	}
	protected void setTotal(Page page){
		page.setTotal(page.getRecords()/page.getRow()+(page.getRecords()%page.getRow()!=0?1:0));
	}
	protected Filters getFilters(){
		String filters=getRequest().getParameter("filters");
		if(isNotEmpty(filters)){
			return filtersToJson(filters);
		}
		return null;
	}
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	protected HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	protected Filters filtersToJson(String jsonString){
		JSONObject jsonObject=JSONObject.fromObject(jsonString);
		String groupOp=jsonObject.get("groupOp").toString();
		JSONArray jsonArray=JSONArray.fromObject(jsonObject.get("rules"));
		List<Rules> rules=JSONArray.toList(jsonArray, Rules.class);
		Filters filters=new Filters();
		filters.setGroupOp(groupOp);
		filters.setRules(rules);
		return filters;
	}
	protected boolean isNotEmpty(Object obj){
		if(obj!=null && !obj.toString().equals("") && !obj.toString().equals("_empty")){
			return true;
		}
		return false;
	}
	protected HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	protected User getSessionUser(){
		Object obj=getSession().getAttribute("user");
		if(isNotEmpty(obj)){
			return (User)obj;
		}
		return null;
	}
}
